/*
 * @(#)ErrorsTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.taglib.html;

import java.util.Iterator;

import javax.servlet.jsp.JspException;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

/**
  ErrorsTag.java

  <p>This class is to extend the ErrorsTag provided by Apache Struts.</p>
  <p>Modification in "struts-html.tld"<br>
  <pre>
     [FROM]
     &lt;tagclass&gt;org.apache.struts.taglib.html.ErrorsTag&lt;/tagclass&gt;

     [TO]
     &lt;tagclass&gt;com.dcivision.framework.taglib.html.ErrorsTag&lt;/tagclass&gt;
  </pre>

    @author          Rollo Chan
    @company         DCIVision Limited
    @creation date   12/07/2003
    @version         $Revision: 1.3 $
*/

public class ErrorsTag extends org.apache.struts.taglib.html.ErrorsTag {

  public static final String REVISION = "$Revision: 1.3 $";


  public ErrorsTag() {
    super();
  }

  public int doStartTag() throws JspException {
    ActionErrors errors = null;
    try {
      errors = RequestUtils.getActionErrors(pageContext, name);
    }
    catch(JspException e) {
      RequestUtils.saveException(pageContext, e);
      throw e;
    }
    if(errors == null || errors.isEmpty()) {
      return 1;
    }
    boolean headerPresent = RequestUtils.present(pageContext, bundle, locale, "errors.header");
    boolean footerPresent = RequestUtils.present(pageContext, bundle, locale, "errors.footer");
    boolean prefixPresent = RequestUtils.present(pageContext, bundle, locale, "errors.prefix");
    boolean suffixPresent = RequestUtils.present(pageContext, bundle, locale, "errors.suffix");
    StringBuffer results = new StringBuffer();
    boolean headerDone = false;
    String message = null;
    for(Iterator reports = property != null ? errors.get(property) : errors.get(); reports.hasNext();) {
      ActionError report = (ActionError)reports.next();
      if(!headerDone) {
        if(headerPresent) {
          message = RequestUtils.message(pageContext, bundle, locale, "errors.header");
          results.append(message);
          results.append(lineEnd);
        }
        headerDone = true;
      }
      if(prefixPresent) {
        message = RequestUtils.message(pageContext, bundle, locale, "errors.prefix");
        results.append(message);
      }
      if (report.getKey() != null && report.getKey().startsWith("customize")) {
        message = RequestUtils.message(pageContext, "customize", locale, report.getKey(), report.getValues());
      } else {
        message = RequestUtils.message(pageContext, bundle, locale, report.getKey(), report.getValues());
      }
      if(message != null) {
        results.append(message);
        results.append(lineEnd);
      }
      if(suffixPresent) {
        message = RequestUtils.message(pageContext, bundle, locale, "errors.suffix");
        results.append(message);
      }
    }

    if(headerDone && footerPresent) {
      message = RequestUtils.message(pageContext, bundle, locale, "errors.footer");
      results.append(message);
      results.append(lineEnd);
    }
    ResponseUtils.write(pageContext, results.toString());
    return 1;
  }

}